package com.djzhou.gmms.service.announcement.impl;

import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.service.announcement.AnnouncementService;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.dao.announcement.AnnouncementDAO;
import com.djzhou.gmms.model.announcement.TbAnnouncement;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.log.TbLog;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.util.DateUtil;

public class AnnouncementServiceImpl extends BaseServiceImpl implements AnnouncementService {

	@Override
	public void add(TbAnnouncement t) {
		announcementDAO.insert(t);
	}

	@Override
	public void delete(Integer id) {
		TbAnnouncement announcement=announcementDAO.queryByID(TbAnnouncement.class, id);
		announcement.setDeleteFlag(1);
		announcementDAO.update(announcement);
		
	}

	@Override
	public TbAnnouncement queryById(Integer id) {
		return announcementDAO.queryByID(TbAnnouncement.class, id);
	}

	@Override
	public List<TbAnnouncement> queryList(HashMap<String, Object> hashMap) {
		return announcementDAO.query("from TbAnnouncement where deleteFlag=0");
	}

	@Override
	public PageBean<TbAnnouncement> queryPageBean(
			HashMap<String, Object> hashMap, int pageSize, int currentPage) {
		String hql="from TbAnnouncement announcement where announcement.deleteFlag=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("startDate")!=null){
				sb.append(" and announcement.startDate>='").append(hashMap.get("startDate").toString()+"'");
			}
			if(hashMap.get("endDate")!=null){
				sb.append(" and announcement.endDate<='").append(DateUtil.turnDateToString(DateUtil.getAfterDate(DateUtil.turnStringToDate(hashMap.get("endDate").toString(), "yyyy-MM-dd"), 1), "yyyy-MM-dd")+"'");
			}
		}
		sb.append(" order by announcement.endDate desc");
		hql=sb.toString();
		PageBean<TbAnnouncement> pageBean=announcementDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbAnnouncement t) {
		announcementDAO.update(t);
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int queryAnnouncementCount() {
		List<TbAnnouncement> list=announcementDAO.query("from TbAnnouncement where deleteFlag=0");
		return list==null?0:list.size();
	}

}
